/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "WMOGroupFile.h"
#include "WMOFile.h"

namespace PQGame
{
	WMOGroupFile::WMOGroupFile(PQEngine::FileBase *f,WMOFile *parser, int index,wmo_mogi *ogi,char* mogn)
		: wmo(parser),file(f),id(index),mogi(ogi),mogp(0),fog(-1),mopys(0),mopyCount(0),movis(0),movts(0),
		movtCount(0),monrs(0),monrCount(0),motvs(0),motvCount(0),mobas(0),mobaCount(0),molrs(0),
		molrCount(0),modrs(0),modrCount(0),mocvs(0),mocvCount(0)
	{
		if(mogi->nameoffset>0){
			name=mogn+mogi->nameoffset;
		}else{
			name=new char[64];
			strcpy(name,"No name");
		}

		if(f!=0){
			init(f);
		}
	}

	void WMOGroupFile::init(PQEngine::FileBase *f)
	{
		f->open();
		char fourcc[5];
		int size=0;
		while(!f->eof()){
			f->read(fourcc,4);
			f->read(&size, 4);
			flipcc(fourcc);
			fourcc[4] = 0;

			if (size == 0){
				continue;
			}

			int nextpos = f->getPosition() + size;
			if (strcmp(fourcc,"MVER")==0) {
				f->read(&version,4);
			}else if (strcmp(fourcc,"MOGP")==0) {
				initMOGP(f,size);
			}else if (strcmp(fourcc,"MOPY")==0) {
				initMOPY(f,size);
			}else if (strcmp(fourcc,"MOVI")==0) {
				initMOVI(f,size);
			}else if (strcmp(fourcc,"MOVT")==0) {
				initMOVT(f,size);
			}else if (strcmp(fourcc,"MONR")==0) {
				initMONR(f,size);
			}else if (strcmp(fourcc,"MOTV")==0) {
				initMOTV(f,size);
			}else if (strcmp(fourcc,"MOBA")==0) {
				initMOBA(f,size);
			}else if (strcmp(fourcc,"MOLR")==0) {
				initMOLR(f,size);
			}else if (strcmp(fourcc,"MODR")==0) {
				initMODR(f,size);
			}else if (strcmp(fourcc,"MOBN")==0) {
			}else if (strcmp(fourcc,"MOBR")==0) {
			}else if (strcmp(fourcc,"MOCV")==0) {
				initMOCV(f,size);	
			}

			f->seek(nextpos);
		}	

		f->close();
	}

	unsigned int* mocvs;
	int mocvCount;
	void WMOGroupFile::initMOCV(PQEngine::FileBase *f,int size)
	{
		mocvCount=(int)size/sizeof(unsigned int);
		mocvs=new unsigned int[mocvCount];
		f->read(mocvs,size);
	}

	void WMOGroupFile::initMODR(PQEngine::FileBase *f,int size)
	{
		modrCount=(int)size/sizeof(short);
		modrs=new short[modrCount];
		f->read(modrs,size);
	}

	void WMOGroupFile::initMOLR(PQEngine::FileBase *f,int size)
	{
		molrCount=(int)size/sizeof(short);
		molrs=new short[molrCount];
		f->read(molrs,size);
	}

	void WMOGroupFile::initMOBA(PQEngine::FileBase *f,int size)
	{
		mobaCount=(int)size/sizeof(wmo_moba);
		mobas=new wmo_moba[mobaCount];
		f->read(mobas,size);
	}

	void WMOGroupFile::initMOTV(PQEngine::FileBase *f,int size)
	{
		motvCount=(int)size/sizeof(Ogre::Vector2);
		motvs=new Ogre::Vector2[motvCount];
		f->read(motvs,size);
	}

	void WMOGroupFile::initMONR(PQEngine::FileBase *f,int size)
	{
		monrCount=(int)size/sizeof(Ogre::Vector3);
		monrs=new Ogre::Vector3[monrCount];
		f->read(monrs,size);
	}

	void WMOGroupFile::initMOVT(PQEngine::FileBase *f,int size)
	{
		movtCount=(int)size/sizeof(Ogre::Vector3);
		movts=new Ogre::Vector3[movtCount];
		f->read(movts,size);
	}

	void WMOGroupFile::initMOVI(PQEngine::FileBase *f,int size)
	{
		moviCount=size/2;
		movis=new unsigned short[moviCount];
		f->read(movis,size);
	}

	void WMOGroupFile::initMOPY(PQEngine::FileBase *f,int size)
	{
		mopyCount=(int)size/sizeof(wmo_mopy);
		mopys=new wmo_mopy[mopyCount];
		f->read(mopys,size);
	}
	void WMOGroupFile::initMOGP(PQEngine::FileBase *f,int size)
	{
		mogp=new wmo_mogp;
		f->read(mogp,sizeof(wmo_mogp));
		wmo_mfog *mfog=wmo->mfogs+mogp->fogs[0];
		if(mfog->r2<=0){
			fog=-1;
		}else{
			fog=mogp->fogs[0];
		}
	}
}
